﻿Public Class Form1
    Private ReadOnly wc As New System.Net.WebClient()
    Private FPath As String = String.Empty
    Public Furl As String = String.Empty
    Public FName As String = String.Empty
    Private sTime As Integer = 0
    Private Sub DownComplete(ByVal sender As System.Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        'sw.WriteLine(String.Format("{0}\rar.exe x {0}\update.rar {1}\ -o+ -ibck -df", Application.StartupPath, FPath))
        If InvokeRequired Then
            Dim d As New dc(AddressOf DownComplete)
            Invoke(d, New Object() {sender, e})
        Else
            sTime = 3
            Label1.Text = "下载完成,准备更新"
            Timer1.Enabled = True
            Timer1.Interval = 1000
            Timer1.Start()
        End If


    End Sub
    Delegate Sub dc(ByVal sender As System.Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    Delegate Sub dw(ByVal sender As System.Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)

    Private Sub DownChanged(ByVal sender As System.Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
        Try
            If InvokeRequired Then
                Dim d As New dw(AddressOf DownChanged)
                Invoke(d, New Object() {sender, e})
            Else
                ProgressBar1.Maximum = e.TotalBytesToReceive
                ProgressBar1.Value = e.BytesReceived
                Label1.Text = String.Format("下载进度:{0}KB/{1}KB", Math.Round(e.BytesReceived / 1024, 2), Math.Round(e.TotalBytesToReceive / 1024, 2))
            End If
        Catch ex As Exception

        End Try


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If System.IO.File.Exists(Application.StartupPath & "\update.rar") Then
            System.IO.File.Delete(Application.StartupPath & "\update.rar")
        End If
        Dim octrl As New BaseClass.Cryp
        Label1.Text = "正在关闭客户端进程..."
        Label1.Text = "初始化..."
        FPath = octrl.desdecryp(System.IO.File.ReadAllText(Application.StartupPath & "\p.ud"), "67771256")
        FName = octrl.desdecryp(System.IO.File.ReadAllText(Application.StartupPath & "\v.ud"), "67784321")
        Dim th As New System.Threading.Thread(AddressOf Dn)
        th.Start()
    End Sub
    Private Sub Dn()
        AddHandler wc.DownloadFileCompleted, AddressOf DownComplete
        AddHandler wc.DownloadProgressChanged, AddressOf DownChanged
        Try
            Dim pr() As Process = Process.GetProcessesByName("BillManage")
            For i As Integer = 0 To pr.Length - 1
                pr(i).Kill()
            Next
            Threading.Thread.Sleep(1000)
        Catch ex As Exception

        End Try
        wc.DownloadFileAsync(New Uri(FName), Application.StartupPath & "\update.rar")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        sTime -= 1
        Label1.Text = String.Format("下载完成,将在{0}秒后更新", sTime)
        If sTime <= 0 Then
            Timer1.Stop()
            Timer1.Enabled = False
            Dim pr As New System.Diagnostics.Process
            pr = System.Diagnostics.Process.Start(Application.StartupPath & "\rar.exe", String.Format(" x ""{0}\update.rar"" ""{1}\"" -o+ -ibck -df", Application.StartupPath, FPath))
            'TextBox1.Text = String.Format(" x """"{0}\update.rar"""" """"{1}\"""" -o+ -ibck -df", Application.StartupPath, FPath)
            ' Return
            While Not pr.HasExited
                Threading.Thread.Sleep(300)
            End While
            System.Diagnostics.Process.Start(FPath & "\BillManage.exe")
            Application.Exit()
        End If

    End Sub
End Class
